
// 购物车状态
export default {
  namespaced: true,
  state: () => {
    return {
      list: []
    }
  },
  //   本地：id skuId name picture price nowPrice count attrsText selected stock isEffective
  // 线上：比上面多 isCollect 有用 discount 无用 两项项信息
  mutations: {
    // 修改参数
    updata (state, goods) {
      const obj = state.list.find(v => v.skuId === goods.skuId)
      for (const key in obj) {
        obj[key] = goods[key]
      }
    },
    // 删除商品
    deletecart (state, goodsid) {
      const index = state.list.findIndex(v => v.skuId === goodsid)
      state.list.splice(index, 1)
    },
    // 添加购物车商品
    insertCart (state, goods) {
      console.log(goods)
      const sameIndex = state.list.findIndex(item => item.skuId === goods.skuId)
      // 逻辑：有相同的给goods累加数量，删除相同skuId的商品
      if (sameIndex >= 0) {
        console.log('youzhegeshangping', sameIndex)
        goods.count = state.list[sameIndex].count + goods.count
        state.list.splice(sameIndex, 1)
      }
      state.list.unshift(goods)
    },
    // 全选
    selected (store, val) {
      console.log(val)
      store.list.forEach(v => {
        v.selected = val
      })
    },
    // 删除选中商品
    deleteall (store) {
      const aa = store.list.filter(v => v.selected !== true)
      store.list = aa
    }
  },
  actions: {
    // 改
    updata (store, goods) {
      return new Promise((resolve, reject) => {
        if (store.rootState.user.token) {
          resolve()
          console.log('修改参数')
        } else {
          store.commit('updata', goods)
          console.log(store.getters.selectall)
          resolve('修改成功')
        }
      })
    },
    // 删
    deletecart (store, goodsid) {
      return new Promise((resolve, reject) => {
        if (store.rootState.user.token) {
          // 有token
          resolve()
        } else {
          store.commit('deletecart', goodsid)
          resolve('删除成功')
        }
      })
    },
    // 删除选中商品
    deleteall (store) {
      return new Promise((resolve, reject) => {
        if (store.rootState.user.token) {
          resolve()
        } else {
          store.commit('deleteall')
          resolve('已删除')
        }
      })
    },
    // 增
    insertCart (ctx, goods) {
      // 使用Promise可以传出一个resolv参数，用.then可以获取这个promise的resolv参数
      return new Promise((resolve, reject) => {
        if (ctx.rootState.user.token) {
          // 已登录 TODO
          resolve()
        } else {
          // 未登录
          ctx.commit('insertCart', goods)
          resolve('添加购物车成功')
        }
      })
    }
  },
  getters: {
    // 总数量
    number (state) {
      let num = 0
      state.list.forEach(v => { num += v.count })
      return num
    },
    // 有效商品总数
    valid (state) {
      let num = 0
      state.list.filter(v => v.stock > 0 && v.isEffective).forEach(v => { num += v.count })
      return num
    },
    // 有效商品总价
    pricealls (state) {
      return state.list.reduce((a, b) => a + (b.count * b.price * 100) / 100, 0)
    },
    // 无效商品
    invalid (state) {
      let num = 0
      state.list.filter(v => v.stock === 0 && !v.isEffective).forEach(v => { num += v.count })
      return num
    },
    // 全选
    selectall (state) {
      const aa = state.list.filter(v => v.selected === true)
      return state.list.length === aa.length && state.list.length > 0
    },
    // 计算选中总价
    priceall (state) {
      const price = state.list.filter(v => v.selected === true)
      const a = price.reduce((a, b) => a + (b.price * b.count * 100) / 100, 0)
      return a
    },
    // 选中数量
    selectnum (state) {
      const select = state.list.filter(v => v.selected === true)
      return select.reduce((a, b) => a + b.count, 0)
    }
  }
}
